package com.sz.admin.dept.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sz.admin.dept.pojo.dto.CompanyCreateDTO;
import com.sz.admin.dept.pojo.dto.CompanyListDTO;
import com.sz.admin.dept.pojo.dto.CompanyUpdateDTO;
import com.sz.admin.dept.pojo.dto.FleetCreateDTO;
import com.sz.admin.dept.pojo.dto.FleetListDTO;
import com.sz.admin.dept.pojo.dto.FleetUpdateDTO;
import com.sz.admin.dept.pojo.vo.CompanyVO;
import com.sz.admin.dept.pojo.vo.FleetVO;
import com.sz.admin.dept.service.DeptService;
import com.sz.core.common.constant.GlobalConstant;
import com.sz.core.common.entity.ApiPageResult;
import com.sz.core.common.entity.ApiResult;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 Controller
 * </p>
 *
 * @author sz-admin
 * @since 2025-10-20
 */
@Tag(name = "部门管理my")
@RestController
@RequestMapping("dept")
@RequiredArgsConstructor
public class DeptController {

    private final DeptService deptService;

    // Company APIs
    @Operation(summary = "新增公司")
    @SaCheckPermission(value = "dept.company.create", orRole = GlobalConstant.SUPER_ROLE)
    @PostMapping("/company")
    public ApiResult<Void> createCompany(@RequestBody CompanyCreateDTO dto) {
        deptService.createCompany(dto);
        return ApiResult.success();
    }

    @Operation(summary = "修改公司")
    @SaCheckPermission(value = "dept.company.update", orRole = GlobalConstant.SUPER_ROLE)
    @PutMapping("/company")
    public ApiResult<Void> updateCompany(@RequestBody CompanyUpdateDTO dto) {
        deptService.updateCompany(dto);
        return ApiResult.success();
    }

    @Operation(summary = "删除公司")
    @SaCheckPermission(value = "dept.company.remove", orRole = GlobalConstant.SUPER_ROLE)
    @DeleteMapping("/company")
    public ApiResult<Void> removeCompanies(@RequestBody SelectIdsDTO dto) {
        deptService.removeCompanies(dto);
        return ApiResult.success();
    }

    @Operation(summary = "分页查询公司")
    @SaCheckPermission(value = "dept.company.query_table", orRole = GlobalConstant.SUPER_ROLE)
    @GetMapping("/company")
    public ApiResult<PageResult<CompanyVO>> pageCompanies(CompanyListDTO dto) {
        return ApiPageResult.success(deptService.pageCompanies(dto));
    }

    @Operation(summary = "分页查询公司")
    @SaCheckPermission(value = "dept.company.query_table", orRole = GlobalConstant.SUPER_ROLE)
    @GetMapping("/company/all")
    public ApiResult<PageResult<CompanyVO>> allCompanies(CompanyListDTO dto) {
        return ApiPageResult.success(deptService.pageCompanies(dto));
    }

    @Operation(summary = "公司详情")
    @SaCheckPermission(value = "dept.company.query_table", orRole = GlobalConstant.SUPER_ROLE)
    @GetMapping("/company/{id}")
    public ApiResult<CompanyVO> detailCompany(@PathVariable Object id) {
        return ApiResult.success(deptService.detailCompany(id));
    }

    // Fleet APIs
    @Operation(summary = "新增车队")
    @SaCheckPermission(value = "dept.fleet.create", orRole = GlobalConstant.SUPER_ROLE)
    @PostMapping("/fleet")
    public ApiResult<Void> createFleet(@RequestBody FleetCreateDTO dto) {
        deptService.createFleet(dto);
        return ApiResult.success();
    }

    @Operation(summary = "修改车队")
    @SaCheckPermission(value = "dept.fleet.update", orRole = GlobalConstant.SUPER_ROLE)
    @PutMapping("/fleet")
    public ApiResult<Void> updateFleet(@RequestBody FleetUpdateDTO dto) {
        deptService.updateFleet(dto);
        return ApiResult.success();
    }

    @Operation(summary = "删除车队")
    @SaCheckPermission(value = "dept.fleet.remove", orRole = GlobalConstant.SUPER_ROLE)
    @DeleteMapping("/fleet")
    public ApiResult<Void> removeFleets(@RequestBody SelectIdsDTO dto) {
        deptService.removeFleets(dto);
        return ApiResult.success();
    }

    @Operation(summary = "分页查询车队")
    @SaCheckPermission(value = "dept.fleet.query_table", orRole = GlobalConstant.SUPER_ROLE)
    @GetMapping("/fleet")
    public ApiResult<PageResult<FleetVO>> pageFleets(FleetListDTO dto) {
        return ApiPageResult.success(deptService.pageFleets(dto));
    }

    @Operation(summary = "分页查询车队")
    @SaCheckPermission(value = "dept.fleet.query_table", orRole = GlobalConstant.SUPER_ROLE)
    @GetMapping("/fleet/all")
    public ApiResult<PageResult<FleetVO>> allFleets(FleetListDTO dto) {
        return ApiPageResult.success(deptService.pageFleets(dto));
    }

    @Operation(summary = "车队详情")
    @SaCheckPermission(value = "dept.fleet.query_table", orRole = GlobalConstant.SUPER_ROLE)
    @GetMapping("/fleet/{id}")
    public ApiResult<FleetVO> detailFleet(@PathVariable Object id) {
        return ApiResult.success(deptService.detailFleet(id));
    }
}